package com.stylefeng.guns.modular.profitLoss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.bank.service.IBankService;
import com.stylefeng.guns.modular.borrowing.service.IBorrowingInfoService;
import com.stylefeng.guns.modular.cash.service.ICashExpenditureService;
import com.stylefeng.guns.modular.cash.service.IDetailsService;
import com.stylefeng.guns.modular.market.service.IMarketService;
import com.stylefeng.guns.modular.profitLoss.model.ProfitLoss;
import com.stylefeng.guns.modular.profitLoss.service.IProfitLossService;
import com.stylefeng.guns.modular.rate.service.IRateService;
import com.stylefeng.guns.modular.revenue.model.Revenue;
import com.stylefeng.guns.modular.revenue.service.IRevenueService;
import com.stylefeng.guns.modular.rmbCashReceipts.service.IRmbCashReceiptsService;
import com.stylefeng.guns.modular.system.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 损益实现类
 * @author laoyu
 * @Date 2018-07-24
 */

@Service
public class ProfitLossServiceImpl  implements IProfitLossService {

    @Autowired
    private IRevenueService revenueService;
    @Autowired
    private IBankService bankService;
    @Autowired
    private IRateService rateService;
    @Autowired
    private ICashExpenditureService cashExpenditureService;
    @Autowired
    private IDetailsService detailsService;
    @Autowired
    private IMarketService marketService;
    @Autowired
    private IRmbCashReceiptsService rmbCashReceiptsService;
    @Autowired
    private IBorrowingInfoService borrowingInfoService;
    /**
     * 获取平台损益报表
     * @param revDate
     * @return
     * @author laoyu
     * @Date 2018-07-24
     */

    @Override
    public List<ProfitLoss> getProfitLoss(String platform, Date revDate) {

        List<ProfitLoss> profitLossList = new ArrayList<>();
        //查询费率
        Rate rate =rateService.search(new SimpleDateFormat("yyyy-MM").format(revDate));
        //计算统计时间
        Date beginDate = DateUtil.getSearchBeginDate(revDate);
        Date endDate = DateUtil.getSearchEndDate(revDate);
        Date yearDate = DateUtil.getSearchYearDate(revDate);

        //根据时间计算总收入
        Revenue revenue = revenueService.getRevenueByMoth(platform,beginDate,endDate);
        double tatol=0;
        if (revenue!=null) {
            tatol = revenue.getAgProfit() + revenue.getBbinProfit() + revenue.getDsProfit() + revenue.getGpProfit() + revenue.getKgProfit()

                    + revenue.getMgProfit() + revenue.getSbProfit() + revenue.getPtProfit() + revenue.getVrProfit() + revenue.getLotterProfit() - revenue.getRebateProfit();
        }
        //年度营收总计
        Revenue revenueAll = revenueService.getRevenueByMoth(platform,yearDate,endDate);
        double allTatol=0;
        if (revenueAll!=null) {
            allTatol = revenueAll.getAgProfit() + revenueAll.getBbinProfit() + revenueAll.getDsProfit() + revenueAll.getGpProfit() + revenueAll.getKgProfit()

                    + revenueAll.getMgProfit() + revenueAll.getSbProfit() + revenueAll.getPtProfit() + revenueAll.getVrProfit() + revenueAll.getLotterProfit() - revenueAll.getRebateProfit();
        }
        ProfitLoss profitLoss = new ProfitLoss();
        profitLoss.setNumber("一");
        profitLoss.setProject("营业收入");
        profitLoss.setRmb(Math.round(tatol));
        profitLoss.setYearTotal(Math.round(allTatol));
        profitLossList.add(profitLoss);
        //获取当月的现金开支
        EntityWrapper<CashExpenditure> wrapper = new EntityWrapper<CashExpenditure>();
        wrapper.eq("platform",platform);
        //组装查询条件
        wrapper.ge("rev_date", beginDate);
        wrapper.lt("rev_date", endDate);
        wrapper.eq("type", 1);
        List<CashExpenditure> cashExpenditures =  cashExpenditureService.selectList(wrapper);
        //营业成本
        double costPISO = 0;
        double costRMB = 0;
        //生活成本
        double livingCostPISO=0;
        double livingCostRMB=0;
        //工资薪酬
        double payrollPISO=0;
        double payrollRMB=0;
        //食堂及补贴
        double canteenPISO = 0;
        double canteenRMB = 0;
        //房租水电网
        double rentPISO=0;
        double rentRMB=0;
        //签证及保关费用
        double visaPISO= 0;
        double visaRMB= 0;
        //机票
        double airTicketsPISO = 0;
        double airTicketsRMB = 0;
        //办公费用
        double officePISO = 0;
        double officeRMB = 0;
        //其他
        double otherPISO=0;
        double otherRMB=0;
        //市场费用
        double marketPISO = 0;
        double marketRMB = 0;
        //市场推广
        double promotionPISO = 0;
        double promotionRMB = 0;
        //手续费
        double feePISO= 0;
        double feeRMB= 0;
        //线路费用
        double linePISO= 0;
        double lineRMB= 0;
        //其他费用
        double otherFeePISO = 0;
        double otherFeeRMB = 0;
        //查询统计现金开支的各种费用
        for (CashExpenditure cashExpenditure:cashExpenditures){
            Details details = detailsService.selectById(cashExpenditure.getDetailsId());
            if (cashExpenditure==null||cashExpenditure.getType() == CashExpenditure.Enum_Type.收入.value||details.getType()==CashExpenditure.Enum_DetailType.借贷.value){
                continue;
            }
            costPISO += cashExpenditure.getMoney();
            if (detailsService.selectById(cashExpenditure.getDetailsId()).getType()==CashExpenditure.Enum_DetailType.生活成本费用.value){
                livingCostPISO += cashExpenditure.getMoney();
                if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.工资薪酬.value){
                    payrollPISO += cashExpenditure.getMoney();
                }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.食堂及补贴.value){
                    canteenPISO += cashExpenditure.getMoney();
                }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.房租费用.value||cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.水费.value
                        ||cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.电费.value||cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.网络费用.value){
                    rentPISO += cashExpenditure.getMoney();
                }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.签证及保关.value){
                    visaPISO += cashExpenditure.getMoney();
                }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.机票费用.value){
                    airTicketsPISO += cashExpenditure.getMoney();
                }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.办公费用.value){
                    officePISO +=cashExpenditure.getMoney();
                }else {
                    otherPISO +=cashExpenditure.getMoney();
                }
            }else if (detailsService.selectById(cashExpenditure.getDetailsId()).getType()==CashExpenditure.Enum_DetailType.市场费用.value){
                marketPISO += cashExpenditure.getMoney();
            }

        }

        //查询rmb支出
        EntityWrapper<RmbCashReceipts> rmbCashReceiptsEntityWrapper = new EntityWrapper<RmbCashReceipts>();
        rmbCashReceiptsEntityWrapper.eq("platform",platform);
        //组装查询条件
        rmbCashReceiptsEntityWrapper.ge("revdate", beginDate);
        rmbCashReceiptsEntityWrapper.lt("revdate", endDate);
        List<RmbCashReceipts> rmbCashReceiptsList = rmbCashReceiptsService.selectList(rmbCashReceiptsEntityWrapper);
        for (RmbCashReceipts rmbCashReceipts:rmbCashReceiptsList){
            if (rmbCashReceipts.getType()==RmbCashReceipts.Enum_Type.收款.value) continue;
            if (detailsService.selectById(rmbCashReceipts.getSummary()).getType()==RmbCashReceipts.Enum_DetailType.其他费用.value)
                otherFeeRMB += rmbCashReceipts.getAmount();
            else if (detailsService.selectById(rmbCashReceipts.getSummary()).getType()==RmbCashReceipts.Enum_DetailType.手续费.value)
                feeRMB += rmbCashReceipts.getAmount();
        }

        //查询应收应付成本
        EntityWrapper<BorrowingInfo> borrowingInfoEntityWrapper = new EntityWrapper<>();
        borrowingInfoEntityWrapper.eq("platform",platform);
        borrowingInfoEntityWrapper.ge("rev_date",beginDate);
        borrowingInfoEntityWrapper.lt("rev_date",endDate);
        List<BorrowingInfo> borrowingInfoList = borrowingInfoService.selectList(borrowingInfoEntityWrapper);
        for (BorrowingInfo borrowingInfo : borrowingInfoList){
            if (borrowingInfo.getCost()<=0) continue;
            if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){//应收为收入故成本要减少
                costPISO -= borrowingInfo.getPisoAmount();
                costRMB -= borrowingInfo.getRmbAmount();
            }
            else{
                costPISO += borrowingInfo.getPisoAmount();
                costRMB += borrowingInfo.getRmbAmount();
            }
            if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.线路费用.value){
                if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value) {
                    linePISO -= borrowingInfo.getPisoAmount();
                    lineRMB -= borrowingInfo.getRmbAmount();
                }else {
                    linePISO += borrowingInfo.getPisoAmount();
                    lineRMB += borrowingInfo.getRmbAmount();
                }
            }else {
                livingCostPISO += borrowingInfo.getRmbAmount() + borrowingInfo.getPisoToRmb();
                if (borrowingInfo.getCost() == BorrowingInfo.Enum_Cost.工资薪酬.value) {
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        payrollPISO -= borrowingInfo.getPisoAmount();
                        payrollRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        payrollPISO += borrowingInfo.getPisoAmount();
                        payrollRMB += borrowingInfo.getRmbAmount();
                    }
                }else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.房租费用.value||borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.水费.value
                        ||borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.电费.value||borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.网络费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        rentPISO -= borrowingInfo.getPisoAmount();
                        rentRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        rentPISO += borrowingInfo.getPisoAmount();
                        rentRMB += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.食堂及补助.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        canteenPISO -= borrowingInfo.getPisoAmount();
                        canteenRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        canteenPISO += borrowingInfo.getPisoAmount();
                        canteenRMB += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.办公费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        officePISO -= borrowingInfo.getPisoAmount();
                        officeRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        officePISO += borrowingInfo.getPisoAmount();
                        officeRMB += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.机票费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        airTicketsPISO -= borrowingInfo.getPisoAmount();
                        airTicketsRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        airTicketsPISO += borrowingInfo.getPisoAmount();
                        airTicketsRMB += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.签证及保关费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        visaPISO -= borrowingInfo.getPisoToRmb();
                        visaRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        visaPISO += borrowingInfo.getPisoToRmb();
                        visaRMB += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.其他杂费.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        otherPISO -= borrowingInfo.getPisoAmount();
                        otherRMB -= borrowingInfo.getRmbAmount();
                    }else {
                        otherPISO += borrowingInfo.getPisoAmount();
                        otherRMB += borrowingInfo.getRmbAmount();
                    }
                }
            }

        }


        //查询市场推广的费用
        EntityWrapper<Market> marketEntityWrapper = new EntityWrapper<Market>();
        marketEntityWrapper.eq("platform",platform);
        //组装查询条件
        marketEntityWrapper.ge("revdate", beginDate);
        marketEntityWrapper.lt("revdate", endDate);
        List<Market> marketList = marketService.selectList(marketEntityWrapper);
        for (Market market:marketList){
            promotionRMB = market.getGroupRanking()+market.getOtherPromotion()+market.getWcDraw()+market.getQq();
        }

        //获取年度的现金开支
        EntityWrapper<CashExpenditure> wrapper1 = new EntityWrapper<CashExpenditure>();
        wrapper1.eq("platform",platform);
        //组装查询条件
        wrapper1.ge("rev_date", yearDate);
        wrapper1.lt("rev_date", endDate);
        wrapper1.eq("type", 1);
        List<CashExpenditure> cashExpendituresAlls =  cashExpenditureService.selectList(wrapper1);
        //年度营业成本
        double costPISOAll = 0;
        double costRMBAll = 0;
        //生活成本
        double livingCostPISOAll = 0;
        double livingCostRMBAll = 0;
        //工资薪酬
        double payrollPISOAll=0;
        double payrollRMBAll=0;
        //食堂及补贴
        double canteenPISOAll = 0;
        double canteenRMBAll = 0;
        //房租水电网
        double rentPISOAll = 0;
        double rentRMBAll = 0;
        //签证及保关费用
        double visaPISOAll = 0;
        double visaRMBAll = 0;
        //机票
        double airTicketsPISOAll = 0;
        double airTicketsRMBAll = 0;
        //办公费用
        double officePISOAll = 0;
        double officeRMBAll = 0;
        //其他
        double otherPISOAll = 0;
        double otherRMBAll = 0;
        //市场推广
        double marketPISOAll = 0;
        double marketRMBAll = 0;
        //市场费用
        double promotionPISOAll = 0;
        double promotionRMBAll = 0;
        //手续费
        double feePISOAll = 0;
        double feeRMBAll = 0;
        //线路
        double linePISOAll = 0;
        double lineRMBAll = 0;
        //其他费用
        double otherFeePISOAll = 0;
        double otherFeeRMBAll = 0;

        for (CashExpenditure cashExpenditure:cashExpendituresAlls){
            Details details = detailsService.selectById(cashExpenditure.getDetailsId());
            if (cashExpenditure==null||cashExpenditure.getType() == CashExpenditure.Enum_Type.收入.value||details.getType()==CashExpenditure.Enum_DetailType.借贷.value){
                continue;
            }
                costPISOAll += cashExpenditure.getMoney();
                if (detailsService.selectById(cashExpenditure.getDetailsId()).getType()==CashExpenditure.Enum_DetailType.生活成本费用.value){
                    livingCostPISOAll += cashExpenditure.getMoney();
                    if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.工资薪酬.value){
                        payrollPISOAll += cashExpenditure.getMoney();
                    }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.食堂及补贴.value){
                        canteenPISOAll += cashExpenditure.getMoney();
                    }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.房租费用.value||cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.水费.value
                            ||cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.电费.value||cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.网络费用.value){
                        rentPISOAll += cashExpenditure.getMoney();
                    }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.签证及保关.value){
                        visaPISOAll += cashExpenditure.getMoney();
                    }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.机票费用.value){
                        airTicketsPISOAll += cashExpenditure.getMoney();
                    }else if (cashExpenditure.getDetailsId()==CashExpenditure.Enum_DetailTypes.办公费用.value){
                        officePISOAll +=cashExpenditure.getMoney();
                    }else {
                        otherPISOAll +=cashExpenditure.getMoney();
                    }
                }else if (detailsService.selectById(cashExpenditure.getDetailsId()).getType()==CashExpenditure.Enum_DetailType.市场费用.value){
                    marketPISOAll += cashExpenditure.getMoney();
                }else if (detailsService.selectById(cashExpenditure.getDetailsId()).getType()==CashExpenditure.Enum_DetailType.其他费用.value){
                    otherFeePISOAll += cashExpenditure.getMoney();
                }

            }

        //查询年度rmb支出
        EntityWrapper<RmbCashReceipts> rmbCashReceiptsEntityWrapperAll = new EntityWrapper<RmbCashReceipts>();
        rmbCashReceiptsEntityWrapperAll.eq("platform",platform);
        //组装查询条件
        rmbCashReceiptsEntityWrapperAll.ge("revdate", yearDate);
        rmbCashReceiptsEntityWrapperAll.lt("revdate", endDate);
        List<RmbCashReceipts> rmbCashReceiptsListAll = rmbCashReceiptsService.selectList(rmbCashReceiptsEntityWrapperAll);
        for (RmbCashReceipts rmbCashReceipts:rmbCashReceiptsListAll){
            if (rmbCashReceipts.getType()==RmbCashReceipts.Enum_Type.收款.value) continue;
            if (detailsService.selectById(rmbCashReceipts.getSummary()).getType()==RmbCashReceipts.Enum_DetailType.其他费用.value)
                otherFeeRMBAll += rmbCashReceipts.getAmount();
            else if (detailsService.selectById(rmbCashReceipts.getSummary()).getType()==RmbCashReceipts.Enum_DetailType.手续费.value)
                feeRMBAll += rmbCashReceipts.getAmount();
        }


        //查询应收应付成本
        EntityWrapper<BorrowingInfo> allBorrowingInfoEntityWrapper = new EntityWrapper<>();
        allBorrowingInfoEntityWrapper.eq("platform",platform);
        allBorrowingInfoEntityWrapper.ge("rev_date",yearDate);
        allBorrowingInfoEntityWrapper.lt("rev_date",endDate);
        List<BorrowingInfo> borrowingInfoListAll = borrowingInfoService.selectList(allBorrowingInfoEntityWrapper);
        for (BorrowingInfo borrowingInfo : borrowingInfoListAll){
            if (borrowingInfo.getCost()<=0) continue;
            if(borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                costPISOAll -= borrowingInfo.getPisoAmount();
                costRMBAll -= borrowingInfo.getRmbAmount();
            }else {
                costPISOAll += borrowingInfo.getPisoAmount();
                costRMBAll += borrowingInfo.getRmbAmount();
            }
            if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.线路费用.value){
                if(borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value) {
                    linePISOAll -= borrowingInfo.getPisoAmount();
                    lineRMBAll -= borrowingInfo.getRmbAmount() + borrowingInfo.getPisoToRmb();
                }else {
                    linePISOAll += borrowingInfo.getPisoAmount();
                    lineRMBAll += borrowingInfo.getRmbAmount() + borrowingInfo.getPisoToRmb();
                }
            }else {
                if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                    livingCostPISOAll -= borrowingInfo.getPisoAmount();
                    lineRMBAll -= borrowingInfo.getRmbAmount();
                }else {
                    livingCostPISOAll += borrowingInfo.getPisoAmount();
                    lineRMBAll += borrowingInfo.getRmbAmount();
                }
                if (borrowingInfo.getCost() == BorrowingInfo.Enum_Cost.工资薪酬.value) {
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        payrollPISOAll -= borrowingInfo.getPisoAmount();
                        payrollRMBAll -= borrowingInfo.getRmbAmount();
                    }else {
                        payrollPISOAll += borrowingInfo.getPisoAmount();
                        payrollRMBAll += borrowingInfo.getRmbAmount();
                    }
                }else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.房租费用.value||borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.水费.value
                        ||borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.电费.value||borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.网络费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        rentPISOAll -= borrowingInfo.getPisoAmount();
                        rentRMBAll -= borrowingInfo.getRmbAmount();
                    }else {
                        rentPISOAll += borrowingInfo.getPisoAmount();
                        rentRMBAll += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.食堂及补助.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        canteenPISOAll -= borrowingInfo.getPisoAmount();
                        canteenRMBAll -= borrowingInfo.getRmbAmount();
                    }else {
                        canteenPISOAll += borrowingInfo.getPisoAmount();
                        canteenRMBAll += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.办公费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        officePISOAll -= borrowingInfo.getPisoAmount();
                        officeRMBAll -= borrowingInfo.getRmbAmount();
                    }else {
                        officePISOAll += borrowingInfo.getPisoAmount();
                        officeRMBAll += borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.机票费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        airTicketsPISOAll -= borrowingInfo.getPisoAmount();
                        airTicketsRMBAll -=borrowingInfo.getRmbAmount();
                    }else {
                        airTicketsPISOAll += borrowingInfo.getPisoAmount();
                        airTicketsRMBAll +=borrowingInfo.getRmbAmount();
                    }

                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.签证及保关费用.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        visaPISOAll-=borrowingInfo.getPisoToRmb();
                        visaRMBAll-=borrowingInfo.getRmbAmount();
                    }else {
                        visaPISOAll+=borrowingInfo.getPisoToRmb();
                        visaRMBAll+=borrowingInfo.getRmbAmount();
                    }
                }
                else if (borrowingInfo.getCost()==BorrowingInfo.Enum_Cost.其他杂费.value){
                    if (borrowingInfo.getType()==BorrowingInfo.Enum_Type.应收.value){
                        otherPISOAll-=borrowingInfo.getPisoAmount();
                        otherRMBAll-=borrowingInfo.getRmbAmount();
                    }else {
                        otherPISOAll+=borrowingInfo.getPisoAmount();
                        otherRMBAll+=borrowingInfo.getRmbAmount();
                    }

                }
            }

        }

        //查询年度市场推广费用
        EntityWrapper<Market> marketEntityWrapperAll = new EntityWrapper<Market>();
        marketEntityWrapperAll.eq("platform",platform);
        //组装查询条件
        marketEntityWrapperAll.ge("revdate", yearDate);
        marketEntityWrapperAll.lt("revdate", endDate);
        List<Market> marketListAll = marketService.selectList(marketEntityWrapperAll);
        for (Market market:marketListAll){
            promotionRMBAll = market.getGroupRanking()+market.getOtherPromotion()+market.getWcDraw()+market.getQq();
        }


        /**
         *
         * 组装excel表中的数据
         */
        //营业成本费用
        ProfitLoss profitLoss1 = new ProfitLoss();
        profitLoss1.setNumber("二");
        profitLoss1.setProject("营业成本费用(正数为成本支出)");
        profitLoss1.setPiso(Math.round(costPISO));
        profitLoss1.setRmb(Math.round(costPISO/rate.getRate()+costRMB));
        profitLoss1.setYearTotal(Math.round(costPISOAll/rate.getRate()+costRMBAll));
        profitLossList.add(profitLoss1);

        //1、生活开支
        ProfitLoss profitLoss2 = new ProfitLoss();
        profitLoss2.setProject("1、生活开支");
        profitLoss2.setPiso(Math.round(livingCostPISO));
        profitLoss2.setRmb(Math.round(livingCostPISO/rate.getRate()+livingCostRMB));
        profitLoss2.setYearTotal(Math.round(livingCostPISOAll/rate.getRate()+livingCostRMBAll));
        profitLossList.add(profitLoss2);
        //（1）员工工资薪酬
        ProfitLoss profitLoss3 = new ProfitLoss();
        profitLoss3.setProject("（1）员工工资薪酬");
        profitLoss3.setPiso(Math.round(payrollPISO));
        profitLoss3.setRmb(Math.round(payrollPISO/rate.getRate()+payrollRMB));
        profitLoss3.setYearTotal(Math.round(payrollPISOAll/rate.getRate()+payrollRMBAll));
        profitLossList.add(profitLoss3);
        //（2）食堂及餐补
        ProfitLoss profitLoss4 = new ProfitLoss();
        profitLoss4.setProject("（2）食堂及餐补");
        profitLoss4.setPiso(Math.round(canteenPISO));
        profitLoss4.setRmb(Math.round(canteenRMB+canteenPISO/rate.getRate()));
        profitLoss4.setYearTotal(Math.round(canteenPISOAll/rate.getRate()+canteenRMBAll));
        profitLossList.add(profitLoss4);
        //（3）宿舍房租水电网
        ProfitLoss profitLoss5 = new ProfitLoss();
        profitLoss5.setProject("（3）宿舍房租水电网");
        profitLoss5.setPiso(Math.round(rentPISO));
        profitLoss5.setRmb(Math.round(rentRMB+rentPISO/rate.getRate()));
        profitLoss5.setYearTotal(Math.round(rentPISOAll/rate.getRate()+rentRMBAll));
        profitLossList.add(profitLoss5);
        //（4）签证及保关费用
        ProfitLoss profitLoss6 = new ProfitLoss();
        profitLoss6.setProject("（4）签证及保关费用");
        profitLoss6.setPiso(Math.round(visaPISO));
        profitLoss6.setRmb(Math.round(visaRMB+visaPISO/rate.getRate()));
        profitLoss6.setYearTotal(Math.round(visaPISOAll/rate.getRate()+visaRMBAll));
        profitLossList.add(profitLoss6);
        //（5）机票费用
        ProfitLoss profitLoss7 = new ProfitLoss();
        profitLoss7.setProject("（5）机票费用");
        profitLoss7.setPiso(Math.round(airTicketsPISO));
        profitLoss7.setRmb(Math.round(airTicketsRMB+airTicketsPISO/rate.getRate()));
        profitLoss7.setYearTotal(Math.round(airTicketsPISOAll/rate.getRate()+airTicketsRMBAll));
        profitLossList.add(profitLoss7);
        //（6）办公费用
        ProfitLoss profitLoss8 = new ProfitLoss();
        profitLoss8.setProject("（6）办公费用");
        profitLoss8.setPiso(Math.round(officePISO));
        profitLoss8.setRmb(Math.round(officeRMB+officePISO/rate.getRate()));
        profitLoss8.setYearTotal(Math.round(officePISOAll/rate.getRate()+officeRMBAll));
        profitLossList.add(profitLoss8);
        //（7）其它
        ProfitLoss profitLoss9 = new ProfitLoss();
        profitLoss9.setProject("（7）其它");
        profitLoss9.setPiso(Math.round(otherPISO));
        profitLoss9.setRmb(Math.round(otherRMB+otherPISO/rate.getRate()));
        profitLoss9.setYearTotal(Math.round(otherPISOAll/rate.getRate()+otherRMBAll));
        profitLossList.add(profitLoss9);
        //2、市场费用
        ProfitLoss profitLoss10 = new ProfitLoss();
        profitLoss10.setProject("2、市场费用");
        profitLoss10.setPiso(Math.round(promotionPISO));
        profitLoss10.setRmb(Math.round(promotionRMB+promotionPISO/rate.getRate()));
        profitLoss10.setYearTotal(Math.round(promotionPISOAll/rate.getRate()+promotionRMBAll));
        profitLossList.add(profitLoss10);
        //3、市场推广
        ProfitLoss profitLoss11 = new ProfitLoss();
        profitLoss11.setProject("3、市场推广");
        profitLoss11.setPiso(Math.round(marketPISO));
        profitLoss11.setRmb(Math.round(marketRMB+marketPISO/rate.getRate()));
        profitLoss11.setYearTotal(Math.round(marketPISOAll/rate.getRate()+marketRMBAll));
        profitLossList.add(profitLoss11);
        //4、手续费
        ProfitLoss profitLoss12 = new ProfitLoss();
        profitLoss12.setProject("4、手续费");
        profitLoss12.setPiso(Math.round(feePISO));
        profitLoss12.setRmb(Math.round(feeRMB+feePISO/rate.getRate()));
        profitLoss12.setYearTotal(Math.round(feePISOAll/rate.getRate()+feeRMBAll));
        profitLossList.add(profitLoss12);

        //线路管理费
        ProfitLoss profitLossLine = new ProfitLoss();
        profitLossLine.setProject("5、线路管理费");
        profitLossLine.setPiso(Math.round(linePISO));
        profitLossLine.setRmb(Math.round(lineRMB+linePISO/rate.getRate()));
        profitLossLine.setYearTotal(Math.round(linePISOAll/rate.getRate()+lineRMBAll));
        profitLossList.add(profitLossLine);
        //5、其他费用
        ProfitLoss profitLoss13 = new ProfitLoss();
        profitLoss13.setProject("6、其他费用");
        profitLoss13.setPiso(Math.round(otherFeePISO));
        profitLoss13.setRmb(Math.round(otherFeeRMB+feePISO/rate.getRate()));
        profitLoss13.setYearTotal(Math.round(otherFeePISOAll/rate.getRate()+otherFeeRMBAll));
        profitLossList.add(profitLoss13);

        //营业利润=营业收入-营业成本（营业成本只按人民币计算）
        ProfitLoss profitLoss14 = new ProfitLoss();
        profitLoss14.setNumber("三");
        profitLoss14.setProject("营业利润");
        profitLoss14.setRmb(Math.round(tatol-(costPISO/rate.getRate()+costRMB)));
        profitLoss14.setYearTotal(Math.round(allTatol-(costPISOAll/rate.getRate()+costRMBAll)));
        profitLossList.add(profitLoss14);
        //营业外收入
        ProfitLoss profitLoss15 = new ProfitLoss();
        profitLoss15.setNumber("四");
        profitLoss15.setProject("营业外收入(废卡解冻)");
        profitLoss15.setRmb(Math.round(bankService.thawMoney(platform,beginDate,endDate)));
        profitLoss15.setYearTotal(Math.round(bankService.thawMoney(platform,yearDate,endDate)));
        profitLossList.add(profitLoss15);
        //营业外收入
        ProfitLoss profitLoss16 = new ProfitLoss();
        profitLoss16.setNumber("五");
        profitLoss16.setProject("营业外支出(废卡冻结)");
        profitLoss16.setRmb(Math.round(bankService.freezeMoney(platform,beginDate,endDate)));
        profitLoss16.setYearTotal(Math.round(bankService.freezeMoney(platform,yearDate,endDate)));
        profitLossList.add(profitLoss16);
        //净利润
        ProfitLoss profitLoss17 = new ProfitLoss();
        profitLoss17.setNumber("六");
        profitLoss17.setProject("净利润");
        profitLoss17.setRmb(Math.round(tatol-(costPISO/rate.getRate()+costRMB)));
        profitLoss17.setYearTotal(Math.round(allTatol-(costPISOAll/rate.getRate()+costRMBAll)));
        profitLossList.add(profitLoss17);


        return profitLossList;
    }

    /**
     * 描述:组装导出算表的数据
     * 创建人: laoyu
     * 日期: 2018/8/1 0001
     * 时间: 下午 4:20
     */
    @Override
    public Map<String, Object> load(List<ProfitLoss> list, String revDate, String platform, String platformName) {
        Map<String, Object> resultmMap = new HashMap<String, Object>();//返回下载数据
        List<Map<String, Object>> loadMapList = new ArrayList<Map<String, Object>>();//下载数据集合
        for (ProfitLoss profitLoss:list) {
            Map<String, Object> loadMap = new HashMap<String, Object>();//下载数据
            if (profitLoss.getNumber()!=null){
                loadMap.put("number", profitLoss.getNumber());
            }else {
                loadMap.put("number",null);
            }
            loadMap.put("project",profitLoss.getProject());
            loadMap.put("piso", profitLoss.getPiso() );
            loadMap.put("rmb",profitLoss.getRmb());
            loadMap.put("yearTotal",profitLoss.getYearTotal());
            loadMapList.add(loadMap);
        }
        resultmMap.put("maplist", loadMapList);
        resultmMap.put("revDate",revDate);
        resultmMap.put("name",platform.toUpperCase()+"损益表");
        resultmMap.put("platform",platform.toUpperCase());
        return resultmMap;
    }
}
